<script setup>
import { ref, onMounted } from 'vue'
import articleEnum from '@/enums/articleEnum.js'
import articleClassifyApi from '@/apis/admin/articleClassifyApi.js'
import articleApi from '@/apis/admin/articleApi.js'
import commonConstant from '@/enums/commonConstant.js'
import PaginationComponent from '@/components/PaginationComponent.vue'
import enumUtil from '@/utils/enumUtil.js'

const articleSearchCondition = ref({
  articleTitle: '',
  articleClassifyIdList: [],
  articleStatus: 1
})
const articleClassifyList = ref([])
const paginationData = ref({ ...commonConstant.PAGINATION })
const articleTableList = ref([])
const drawerData = ref({
  visible: false,
  title: ''
})
const waitUpdateArticleData = ref({})

onMounted(async () => {
  await handleSelectArticle()
})

// 查询文章
const handleSelectArticle = async () => {
  const response = await articleApi.pageSelectArticle(
    paginationData.value.currentPage,
    paginationData.value.pageSize,
    articleSearchCondition.value
  )
  articleTableList.value = response.records
  paginationData.value.total = response.total
  await handleSelectArticleClassify()
}

// 查询文章分类
const handleSelectArticleClassify = async () => {
  const response = await articleClassifyApi.selectArticleClassify(null)
  articleClassifyList.value = response
}

// 表格选择发送改变
const handleSelectionChange = (newSelection) => {
  console.log(newSelection)
}

// 提交抽屉（完成新增或修改）
const handleSubmitDrawer = async () => {
  await handleUpdateArticle()
  await handleCloseDrawer()
}

// 修改文章
const handleUpdateArticle = async () => {
  await ElMessageBox.confirm(`是否确认执行该操作?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async () => {
      await articleApi.updateArticle(waitUpdateArticleData.value)
      await handleSelectArticle()
    })
    .catch(() => {})
}

// 打开抽屉
const handleOpenDrawer = (data) => {
  waitUpdateArticleData.value = { ...data }
  drawerData.value.title = '更新文章'
  drawerData.value.visible = true
}

// 关闭抽屉
const handleCloseDrawer = () => {
  waitUpdateArticleData.value = {}
  drawerData.value.visible = false
}

// 切换页面大小
const handleSizeChange = (value) => {
  paginationData.value.pageSize = value
  handleSelectArticle()
}

// 切换页码
const handleCurrentChange = (value) => {
  paginationData.value.currentPage = value
  handleSelectArticle()
}
</script>
<template>
  <div class="wait-audit-article-manage">
    <div class="search-condition-item">
      <el-form :inline="true" :model="articleSearchCondition">
        <el-form-item label="文章标题">
          <el-input
            v-model="articleSearchCondition.articleTitle"
            placeholder="请输入"
            clearable
          />
        </el-form-item>
        <el-form-item label="文章分类">
          <el-select
            v-model="articleSearchCondition.articleClassifyIdList"
            placeholder="请选择"
            clearable
            multiple
            collapse-tags
          >
            <el-option
              v-for="item in articleClassifyList"
              :key="item.articleClassifyId"
              :label="item.articleClassifyName"
              :value="item.articleClassifyId"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSelectArticle">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="table-item">
      <el-table
        :data="articleTableList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="articleTitle" label="文章标题" width="140" />
        <el-table-column
          prop="user.userNickname"
          label="文章作者用户昵称"
          width="140"
        />
        <el-table-column
          prop="user.username"
          label="文章作者用户名"
          width="140"
        />
        <el-table-column
          prop="articleClassifyList"
          label="文章分类"
          width="140"
        >
          <template #default="scope">
            <div
              v-if="
                scope.row.articleClassifyList &&
                scope.row.articleClassifyList.length > 0
              "
            >
              <el-tag
                type="primary"
                v-for="item in scope.row.articleClassifyList"
                :key="item.articleClassifyId"
              >
                {{ item.articleClassifyName }}
              </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="articleStatus" label="文章状态">
          <template #default="scope">
            {{
              enumUtil.getLableByValue(
                articleEnum.articleStatusEnum,
                scope.row.articleStatus
              )
            }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="220" />
        <el-table-column prop="updateTime" label="更新时间" width="220" />
        <el-table-column fixed="right" label="操作" min-width="120">
          <template #default="scope">
            <el-button
              link
              type="primary"
              size="small"
              @click="handleOpenDrawer(scope.row)"
            >
              审核
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-item">
      <PaginationComponent
        :paginationData="paginationData"
        @sizeChange="handleSizeChange"
        @currentChange="handleCurrentChange"
      ></PaginationComponent>
    </div>

    <div class="drawer-item">
      <el-drawer
        v-model="drawerData.visible"
        :show-close="false"
        @close="handleCloseDrawer"
      >
        <template #header>
          <h4>{{ drawerData.title }}</h4>
        </template>
        <el-form label-width="auto" :model="waitUpdateArticleData">
          <el-form-item label="文章状态">
            <el-select
              v-model="waitUpdateArticleData.articleStatus"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="item in articleEnum.articleStatusEnum"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="文章审核失败原因"
            v-if="waitUpdateArticleData.articleStatus === 3"
          >
            <el-input
              v-model="waitUpdateArticleData.articleAuditFailMessage"
              placeholder="请输入"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleCloseDrawer">取消</el-button>
            <el-button type="primary" @click="handleSubmitDrawer">
              确认</el-button
            >
          </el-form-item>
        </el-form>
      </el-drawer>
    </div>
  </div>
</template>
<style scoped>
.search-condition-item .el-input {
  width: 210px;
}
.search-condition-item .el-select {
  width: 210px;
}
.el-table {
  width: 100%;
  height: 483px;
}
</style>
